﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
using StayUnlocked.Properties;

namespace StayUnlocked
{
    public partial class Form1 : Form
    {
        private bool _locked = false;
        private bool _minimize = false;

        public Form1(bool minimize)
        {
            InitializeComponent();
            _minimize = minimize;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (_minimize)
                Visible = false;

            radioLockComputer.Checked = Settings.Default.LockComputer;
            numLockMinutes.Value = Settings.Default.LockMinutes;
            numLockMinutes.Enabled = radioLockComputer.Checked;

            radioAllowLocking.Checked = Settings.Default.AllowLocking;
            numAllowMinutes.Value = Settings.Default.AllowMinutes;
            numAllowMinutes.Enabled = radioAllowLocking.Checked;

            radioUnlockedForever.Checked = Settings.Default.UnlockedForever;

            checkAutoStart.Checked = Shortcut.StartupFolderShortcutExists();

            CheckIdleTimer.Start();

            SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
        }

        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            switch (e.Reason)
            {
                case SessionSwitchReason.SessionLock:
                    _locked = true;
                    break;
                case SessionSwitchReason.SessionUnlock:
                    _locked = false;
                    break;
            }
        }

        private void CheckIdleTimer_Tick(object sender, EventArgs e)
        {
            var idleTime = Win32.GetIdleTime();
            var ts = new TimeSpan(0, 0, 0, 0, (int)idleTime);

            IdleTime.Text =
                "Idle time : " + String.Format("{0}.{1:00}:{2:00}:{3:00}", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);

            if (_locked)
                return;

            if (Settings.Default.LockComputer)
            {
                if ((double)Settings.Default.LockMinutes < ts.TotalMinutes)
                    Win32.LockWorkStation();
                else
                    Win32.SetThreadExecutionState(Win32.EXECUTION_STATE.ES_DISPLAY_REQUIRED);
            }
            else if (Settings.Default.AllowLocking)
            {
                if ((double)Settings.Default.AllowMinutes > ts.TotalMinutes)
                    Win32.SetThreadExecutionState(Win32.EXECUTION_STATE.ES_DISPLAY_REQUIRED);
            }
            else if (Settings.Default.UnlockedForever)
            {
                Win32.SetThreadExecutionState(Win32.EXECUTION_STATE.ES_DISPLAY_REQUIRED);
            }
        }

        private void radioLockComputer_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.LockComputer = radioLockComputer.Checked;
            Settings.Default.Save();
            numLockMinutes.Enabled = radioLockComputer.Checked;
        }

        private void numLockMinutes_ValueChanged(object sender, EventArgs e)
        {
            if (numLockMinutes.Value < 1.0m)
                numLockMinutes.Value = 1.0m;
            Settings.Default.LockMinutes = numLockMinutes.Value;
            Settings.Default.Save();
        }

        private void radioAllowLocking_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.AllowLocking = radioAllowLocking.Checked;
            Settings.Default.Save();
            numAllowMinutes.Enabled = radioAllowLocking.Checked;
        }

        private void numAllowMinutes_ValueChanged(object sender, EventArgs e)
        {
            if (numAllowMinutes.Value < 1.0m)
                numAllowMinutes.Value = 1.0m;
            Settings.Default.AllowMinutes = numAllowMinutes.Value;
            Settings.Default.Save();
        }

        private void radioUnlockedForever_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.UnlockedForever = radioUnlockedForever.Checked;
            Settings.Default.Save();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
                Visible = false;
        }

        private void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Thread.Sleep(200);
            Visible = true;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var startInfo = new ProcessStartInfo("explorer.exe", "https://stayunlocked.codeplex.com");
            Process.Start(startInfo);
        }

        private void checkAutoStart_CheckedChanged(object sender, EventArgs e)
        {
            if (checkAutoStart.Checked) 
                Shortcut.CreateStartupFolderShortcut();
            else 
                Shortcut.DeleteStartupFolderShortcuts();
        }

        private void Form1_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                WindowState = FormWindowState.Normal;
                Activate();
            }
        }
    }
}
